#!/bin/bash

# $Header: /opt/unixmon/tools/DNS/RCS/named_sync_masters,v 1.1 2012/11/27 07:50:51 yajin Exp yajin $

# Author: yang.jin@ebay.com
# the script copies the named configuration, master zones from 
# the primary master DNS server to secondary master DNS servers.

print_usage()
{

echo -e '

named_sync_masters - distribute configuration for zones and master zone DBs to secondary master DNS servers.

Usage:
        named_sync_masters -s sites [-d secondary-master-servers]

        "sites" could be "all", otherwise, one or combination of the following:
                its
                mp
                base
                misc
                qa
                nodb

        "secondary-master-servers" could be "all", otherwise, one or combination of pseudo masters defined on /etc/named/IT_DNS_SERVERS.

Examples:

  Propagate changes to misc.zones and its.zones onto sjcitdns02 and denitdns06 -
        named_sync_masters -s "its misc" -d "sjcitdns02 denitdns06.den.its.ebay.com"

  Propagate changes to its.zones onto all secondary master servers -
        named_sync_masters -s its -d all
    or,
        named_sync_masters -s its 
' >&2
        
}

export PATH=/bin:/usr/bin:/usr/local/bin
# --------------------------------------
# for DEBUG purpose
# ROOT="/MINE"
## RNDC="echo /opt/bind/sbin/rndc"

# for PROD purpose
ROOT=""
RNDC="/opt/bind/sbin/rndc"
ISPRI="/usr/local/sbin/is_primaster"
# --------------------------------------

if [ "X$(uname -s)" == "XSunOS" ]; then
    GREP="/usr/xpg4/bin/grep"
    SED="/usr/xpg4/bin/sed"
else
    GREP="/bin/grep"
    SED="/bin/sed"
fi

CONF_DIR="${ROOT}/etc/named"
ZONE_DIR="${ROOT}/var/named/db"

DNS_SERVERS_LIST="${CONF_DIR}/IT_DNS_SERVERS"

SITES="its mp base misc qa nodb"
DBS="its"


# this tool is suppposed to be executed on the primary DNS master
if [ ! -x $ISPRI ]; then
	echo "$ISPRI does not exist, or, is not executable." >&2
	exit 2
fi

$ISPRI
if [ $? -ne 0 ]; then 
	echo "This host is not the primary DNS master."  >&2
	exit 2
fi


len=$#
if [ $len -ne 2 ] && [ $len -ne 4 ]; then print_usage; exit 1; fi

option1=$1
para1=$2
option2=$3
para2=$4 

if [ $len -eq 2 ]; then option2="-d"; para2="all"; fi

if [ "X${option1}" != "X-s" ] || [ "X${option2}" != "X-d" ]; then print_usage; exit 1; fi

if [ "X${para1}" == "Xall" ]; then 
	sites="$SITES"
else
	sites="$para1"	
fi

cf=$(echo $sites | $SED -e 's/^[[:space:]]*//' -e 's/$/ /' | $SED -e "s/[[:space:]]\{1,\}/\.zones /g" | $SED -e 's/[[:space:]]*$//')


if [ "$para2" == "all" ]; then
	. $DNS_SERVERS_LIST
	export DNS_PSEUDO_MASTERS
else
	DNS_PSEUDO_MASTERS="$para2"; export DNS_PSEUDO_MASTERS
fi


for pseudo in $DNS_PSEUDO_MASTERS; do

	ssh $pseudo "if [ ! -d $CONF_DIR ]; then mkdir -m 755 -p $CONF_DIR; fi; if [ ! -d $ZONE_DIR ]; then mkdir -p $ZONE_DIR; fi"

	echo -e "\nSynchronizing named configuration($cf) under $CONF_DIR to $pseudo... "
	cd $CONF_DIR; tar cf - $cf | ssh $pseudo "cd $CONF_DIR; tar xf -"

	cd ${ZONE_DIR}
	for site in $sites; do 
		if echo $DBS | $GREP -q -w $site; then
			echo -e "Synchronizing master zone DBs under ${ZONE_DIR}/${site} to $pseudo... "
			rsync -a ${ZONE_DIR}/${site} ${pseudo}:${ZONE_DIR}
		fi
	done

	echo -e "Reloading named configuration on $pseudo... "
	ssh $pseudo "$RNDC reconfig"

done

